﻿using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;

namespace Game.FlowField.Burst
{
    [BurstCompile]
    public struct BlockAddingJob : IJob
    {
        [ReadOnly]
        public int2 MapSize;

        /// <summary>
        /// Input
        /// </summary>
        [ReadOnly]
        public FlexibleArray<int2> AddingGridIndex2Ds;

        /// <summary>
        /// Output
        /// </summary>
        [WriteOnly]
        public NativeArray<bool> GridIsBlocks;

        /// <summary>
        /// Output
        /// </summary>
        public FlexibleArray<Unit> Units;

        [BurstDiscard]
        public void WrapData(MapData mapData, FlexibleArray<Unit> units)
        {
            this.MapSize = mapData.Size;
            this.GridIsBlocks = mapData.GridIsBlocks;
            this.Units = units;
        }

        [BurstCompile]
        public void Execute()
        {
            var length = AddingGridIndex2Ds.Length;
            var array = AddingGridIndex2Ds.Array;
            for (int i = 0; i < length; i++)
            {
                var addingIndex2D = array[i];
                if (!MapDataUtil.IsInsideMap(addingIndex2D, this.MapSize))
                    continue;

                var index = MapDataUtil.ConvertIndex2D2Index(addingIndex2D, this.MapSize);
                this.GridIsBlocks[index] = true;


            }
        }
    }
}